package com.commander.spring.cloud.config.sentinel.rule;

import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * description:
 * <p>
 * Date: 2020-07-06
 * <p>
 * Time: 18:09
 *
 * @author zhangxin
 */
@Component
public class SentinelRuleInitConfig implements CommandLineRunner {
	
	@Override
	public void run(String... args) throws Exception {
//		initFlowRule();
//		initDegradeRule();
//		initSystemRule();
//		initAuthRule();
//		initParamFlowRule();
	}
	
	@Qualifier(NacosRuleConfig.FLOW_RULE_NACOS_CONFIG)
	@Autowired
	private NacosDataSourceProperties flowRuleNacosConfig;
	private void initFlowRule(){
		Converter<String, List<FlowRule>> parser = source -> JSON.parseObject(source,new TypeReference<List<FlowRule>>() {});
		ReadableDataSource<String, List<FlowRule>> nacosDataSource = new NacosDataSource<>(flowRuleNacosConfig.getServerAddr(), flowRuleNacosConfig.getGroupId(), flowRuleNacosConfig.getDataId(), parser);
		FlowRuleManager.register2Property(nacosDataSource.getProperty());
	}
	
	@Qualifier(NacosRuleConfig.DEGRADE_RULE_NACOS_CONFIG)
	@Autowired
	private NacosDataSourceProperties degradeRuleNacosConfig;
	private void initDegradeRule(){
		Converter<String, List<DegradeRule>> parser = source -> JSON.parseObject(source,new TypeReference<List<DegradeRule>>() {});
		ReadableDataSource<String, List<DegradeRule>> nacosDataSource = new NacosDataSource<>(degradeRuleNacosConfig.getServerAddr(), degradeRuleNacosConfig.getGroupId(), degradeRuleNacosConfig.getDataId(), parser);
		DegradeRuleManager.register2Property(nacosDataSource.getProperty());
	}
	
	@Qualifier(NacosRuleConfig.SYSTEM_RULE_NACOS_CONFIG)
	@Autowired
	private NacosDataSourceProperties systemRuleNacosConfig;
	private void initSystemRule(){
		Converter<String, List<SystemRule>> parser = source -> JSON.parseObject(source,new TypeReference<List<SystemRule>>() {});
		ReadableDataSource<String, List<SystemRule>> nacosDataSource = new NacosDataSource<>(systemRuleNacosConfig.getServerAddr(), systemRuleNacosConfig.getGroupId(), systemRuleNacosConfig.getDataId(), parser);
		SystemRuleManager.register2Property(nacosDataSource.getProperty());
	}
	
	@Qualifier(NacosRuleConfig.AUTH_RULE_NACOS_CONFIG)
	@Autowired
	private NacosDataSourceProperties authRuleNacosConfig;
	private void initAuthRule(){
		Converter<String, List<AuthorityRule>> parser = source -> JSON.parseObject(source,new TypeReference<List<AuthorityRule>>() {});
		ReadableDataSource<String, List<AuthorityRule>> nacosDataSource = new NacosDataSource<>(authRuleNacosConfig.getServerAddr(), authRuleNacosConfig.getGroupId(), authRuleNacosConfig.getDataId(), parser);
		AuthorityRuleManager.register2Property(nacosDataSource.getProperty());
	}
	
	@Qualifier(NacosRuleConfig.PARAM_FLOW_RULE_NACOS_CONFIG)
	@Autowired
	private NacosDataSourceProperties paramFlowRuleNacosConfig;
	private void initParamFlowRule(){
		Converter<String, List<ParamFlowRule>> parser = source -> JSON.parseObject(source,new TypeReference<List<ParamFlowRule>>() {});
		ReadableDataSource<String, List<ParamFlowRule>> nacosDataSource = new NacosDataSource<>(paramFlowRuleNacosConfig.getServerAddr(), paramFlowRuleNacosConfig.getGroupId(), paramFlowRuleNacosConfig.getDataId(), parser);
		ParamFlowRuleManager.register2Property(nacosDataSource.getProperty());
	}
}
